#include "HardwareProfile.h"

int __attribute__((space(eedata))) eeData;
void eeprom_write(unsigned char add,unsigned int data)
{
	unsigned int offset;
	NVMCON = 0x4004;
	TBLPAG = __builtin_tblpage(&eeData);        //Initialize eeprom address
	offset = __builtin_tbloffset(&eeData);
	offset = offset+add;
	__builtin_tblwtl(offset,data);
	asm volatile("disi,#5");
	__builtin_write_NVM();
	while(NVMCONbits.WR);
}

unsigned int eeprom_read(unsigned char add)
{
	unsigned int offset;
	unsigned int data;
//	NVMCON = 0x4004;
	TBLPAG = __builtin_tblpage(&eeData);        //Initialize eeprom address
	offset = __builtin_tbloffset(&eeData);
	offset = offset+add;
	data   = __builtin_tblrdl(offset);
//	asm volatile("disi,#5");
//	__builtin_write_NVM();
//	while(NVMCONbits.WR);
//	NVMCON = 0x4004;
	return data;
}
